package library.view;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import library.dao.Impl.BookTypeDaoImpl;
import library.pojo.Book;
import library.pojo.BorrowHistory;
import library.pojo.User;
import library.service.LibraryService;

public class CommonSearchPanel extends JPanel {
	private JTextField bookSearchTextField;
	private JTable table;
	private LibraryService libraryService;
	private JScrollPane tablePanel;
	private JComboBox searchTypeComboBox;
	private JScrollPane scrollPane;
	private List<Book> bookList = null;
	private Iterator<Book> iter;
	private User user;
	private BorrowHistory bh = null;
	private int selRow = -1;
	private ImageIcon icon;
	private Image img;

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		g.drawImage(img,0,0,this.getWidth(),this.getHeight(),this);
	}
	
	public CommonSearchPanel(List<Book> bookList) {
		setBounds(100, 100, 1200, 750);
		this.setLayout(null);

		icon = new ImageIcon("res/2.png");
		img = icon.getImage();
		
		JLabel message = new JLabel("请选择查询类型:");
		message.setFont(new Font("宋体", Font.PLAIN, 18));
		message.setBounds(189, 32, 248, 18);
		this.add(message);

		String[] ways = { "按照isbn查询","按照书名查询", "按照作者查询","按照图书状态查询" };
		searchTypeComboBox = new JComboBox(ways);
		searchTypeComboBox.setFont(new Font("宋体", Font.PLAIN, 14));
		searchTypeComboBox.setToolTipText("");
		searchTypeComboBox.setBounds(189, 63, 248, 24);
		this.add(searchTypeComboBox);

		bookSearchTextField = new JTextField();
		bookSearchTextField.setBounds(514, 63, 295, 24);
		this.add(bookSearchTextField);
		bookSearchTextField.setColumns(10);

		JButton searchButton = new JButton("查询");
		searchButton.setIcon(new ImageIcon("res/search.png"));
		searchButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				bookSearchActionPerformed(e);
			}
		});
		searchButton.setActionCommand("searchPost");
		searchButton.setFont(new Font("宋体", Font.PLAIN, 18));
		searchButton.setBounds(830, 62, 99, 27);
		this.add(searchButton);


		scrollPane = new JScrollPane();
		scrollPane = this.getContactJPanel(bookList);
		scrollPane.setBounds(189, 120, 740, 478);
		this.add(scrollPane);

	}

	public JScrollPane getContactJPanel(List<Book> bookList) {
		selRow = -1;
		String name[] = { "isbn", "书名", "作者", "出版日期", "图书类型", "图书状态" };
		Object data[][] = new Object[bookList.size()][7];
		this.bookList = bookList;
		iter = bookList.iterator();
		int i = 0;
		while (iter.hasNext()) {
			BookTypeDaoImpl bookTypeDao = new BookTypeDaoImpl();
			Book book = iter.next();
			data[i][0] = book.getIsbn();
			data[i][1] = book.getBookName();
			data[i][2] = book.getAuthor();
			data[i][3] = book.getPublicationYear();
			data[i][4] = bookTypeDao.getTypeName(book.getBookTypeIdx());
			data[i][5] = book.getState();
			i++;
		}
		DefaultTableModel tableModel = new DefaultTableModel(data, name);
		table = new JTable(tableModel);
		table.getTableHeader().setFont(new Font("宋体", Font.BOLD, 16)); // 表头字体
		table.setFont(new Font("微软雅黑", Font.PLAIN, 14)); // 表体字体
		table.getTableHeader().setPreferredSize(new Dimension(1, 30)); // 表头高度
		table.setRowHeight(table.getRowHeight() + 15); // 表体高度
		table.repaint();
		tablePanel = new JScrollPane(table);
		table.addMouseListener(new MouseAdapter() {
			public void mouseClicked(final MouseEvent e) {
				selRow = table.getSelectedRow(); // 得到选中的行
				if(selRow>=0) {
					bh = new BorrowHistory(bookList.get(selRow).getIsbn(), bookList.get(selRow).getBookName(),
							null, getStringDateShort(), "借出",60,0);}
				System.out.println(selRow);
				System.out.println(bh);
			}
		});

		return tablePanel;
	}

	public void bookSearchActionPerformed(ActionEvent evt) {
		if (searchTypeComboBox.getSelectedItem().equals("按照isbn查询")) {
			String isbnInput = bookSearchTextField.getText().trim();
			libraryService = new LibraryService();
			bookList = libraryService.getSearchIsbn(isbnInput);
		}
		if (searchTypeComboBox.getSelectedItem().equals("按照书名查询")) {
			String bookNameInput = bookSearchTextField.getText().trim();
			libraryService = new LibraryService();
			bookList = libraryService.getBook(bookNameInput);
		}
		if (searchTypeComboBox.getSelectedItem().equals("按照作者查询")) {
			String authorNameInput = bookSearchTextField.getText().trim();
			libraryService = new LibraryService();
			bookList = libraryService.getAuthorBook(authorNameInput);
		}
		if (searchTypeComboBox.getSelectedItem().equals("按照图书状态查询")) {
			String stateInput = bookSearchTextField.getText().trim();
			libraryService = new LibraryService();
			bookList = libraryService.getStateBook(stateInput);
		}

		selRow = -1;
		String name[] = { "isbn", "书名", "作者", "出版日期", "图书类型", "图书状态" };
		Object data[][] = new Object[bookList.size()][7];
		iter = bookList.iterator();
		int i = 0;
		while (iter.hasNext()) {
			BookTypeDaoImpl bookTypeDao = new BookTypeDaoImpl();
			Book book = iter.next();
			data[i][0] = book.getIsbn();
			data[i][1] = book.getBookName();
			data[i][2] = book.getAuthor();
			data[i][3] = book.getPublicationYear();
			data[i][4] = bookTypeDao.getTypeName(book.getBookTypeIdx());
			data[i][5] = book.getState();
			i++;
		}
		DefaultTableModel tableModel = new DefaultTableModel(data, name);
		table = new JTable(tableModel);
		table.getTableHeader().setFont(new Font("宋体", Font.BOLD, 16)); // 表头字体
		table.setFont(new Font("微软雅黑", Font.PLAIN, 14)); // 表体字体
		table.getTableHeader().setPreferredSize(new Dimension(1, 30)); // 表头高度
		table.setRowHeight(table.getRowHeight() + 15); // 表体高度
		table.repaint();
		tablePanel = new JScrollPane(table);
		table.addMouseListener(new MouseAdapter() {
			public void mouseClicked(final MouseEvent e) {
				selRow = table.getSelectedRow(); // 得到选中的行
				if(selRow>=0) {
				bh = new BorrowHistory(bookList.get(selRow).getIsbn(), bookList.get(selRow).getBookName(),
						null, getStringDateShort(), "借出",60,0);}
			}
		});
		this.remove(scrollPane);
		scrollPane = tablePanel;
		scrollPane.setBounds(189, 120, 740, 478);
		this.add(scrollPane);
		this.validate();
	}

	public Iterator<Book> getIter() {
		return iter;
	}

	public void setIter(Iterator<Book> iter) {
		this.iter = iter;
	}

	public String getStringDateShort() {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		String dateString = formatter.format(currentTime);
		return dateString;
	}
}
